<?php
namespace App\Controllers\Common;

use \App\Libs\DB\PailePdo;
class GetOrderCount extends \App\Libs\Base\BaseAction
{
	protected function beforeRun() {
        parent::beforeRun();
        $this->needLogin = TRUE;
    }
	public function execute(){
		$uid = $this->session->user->id;
		//$uid =131;
		$sql = "SELECT status,count(*) count FROM t_paile_order WHERE uid = {$uid} GROUP BY `status` order by status";
		$result = PailePdo::findAll($sql);
		$nowTime = time();
		$status0sql ="SELECT count(*) count FROM t_paile_order WHERE uid = {$uid} and status=0 and unix_timestamp(stime) > {$nowTime}";
		$count0 = PailePdo::findAll($status0sql)[0]['count'];
		if( empty( $result ) ){
			return \App\Libs\Views\Json::view(0, '成功',['one'=>0,'zone'=>0,'five'=>0,'six'=>0] );	
		}else{
			foreach( $result as $k=>$v){
				if( !isset($info['six']) ){
					$info['six'] = 0;
				}
				if( !isset($info['five']) ){
					$info['five'] = 0;
				}
				if( !isset($info['one']) ){
					$info['one'] = 0;
				}
				if( !isset($info['zone']) ){
					$info['zone'] = 0;
				}
				switch( $v['status'] ){
					case 0:
						$info['zone'] = $count0;
						break;
					case 1:
						$info['one'] = $v['count'];
						break;
					case 6:
						$info['six'] += $v['count'];
						break;
					case 7:
						$info['six'] += $v['count'];
						break;
					case 5:
						$info['five'] += $v['count'];
						break;
					case 8:
						$info['five'] += $v['count'];
						break;
					default:
						$info[$v['status']] = $v['count'];
						break;
				}
			}
		}
        return \App\Libs\Views\Json::view(0, '成功', $info);
    }

}
